package com.iceframework.core.utils;

import org.springframework.jms.core.JmsTemplate;

import com.alibaba.druid.support.json.JSONUtils;
import com.iceframework.core.service.MQService;

/**
 * MQ工具类
 * 
 * @author Palo
 *
 */
public class MQUtils {

	private static MQService mQService;

	static {
		mQService = SpringContextUtils.getBean("mQService", MQService.class);
	}

	/**
	 * 发送消息
	 * 
	 * @param channel 信道
	 * @param data    数据 （非字符串需转换JSON String）
	 * @throws Exception
	 */
	public static void send(String channel, String data) throws Exception {
		mQService.sender(channel, data);
	}

	public static <T> T receive(String channel, Class<T> clazz) throws Exception {
		JmsTemplate jmsTemplate = SpringContextUtils.getBean("jmsTemplate", JmsTemplate.class);
		String obj = JSONUtils.toJSONString(jmsTemplate.receiveAndConvert(channel));
		return (T) JsonUtils.convertString2Obj(obj, clazz);
	}
}
